package com.jastar.android.test.demo;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.jastar.android.test.R;

public class CalculatorActivity extends AppCompatActivity implements KeyEvent.Callback, View.OnClickListener {

    private String number1 = "0";
    private String operation = "";
    private String number2 = "0";
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        tv_result = findViewById(R.id.tv_result);

        findViewById(R.id.button_7).setOnClickListener(this);
        findViewById(R.id.button_8).setOnClickListener(this);
        findViewById(R.id.button_9).setOnClickListener(this);
        findViewById(R.id.button_4).setOnClickListener(this);
        findViewById(R.id.button_5).setOnClickListener(this);
        findViewById(R.id.button_6).setOnClickListener(this);
        findViewById(R.id.button_1).setOnClickListener(this);
        findViewById(R.id.button_2).setOnClickListener(this);
        findViewById(R.id.button_3).setOnClickListener(this);
        findViewById(R.id.button_0).setOnClickListener(this);
        findViewById(R.id.button_back).setOnClickListener(this);
        findViewById(R.id.button_clear).setOnClickListener(this);
        findViewById(R.id.button_plus).setOnClickListener(this);
        findViewById(R.id.button_minus).setOnClickListener(this);
        findViewById(R.id.button_equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        String text = String.valueOf(((Button) v).getText());

        if (id == R.id.button_back) {

            if (operation.isEmpty()) {
                number1 = number1.substring(0, number1.length() - 1);
                if (number1.isEmpty()) number1 = "0";
                onClick_refreshText(null);
            } else {
                number2 = number2.substring(0, number2.length() - 1);
                if (number2.isEmpty()) number2 = "0";
                onClick_refreshText(null);
            }

        } else if (id == R.id.button_clear) {

            number1 = "0";
            operation = "";
            number2 = "0";
            onClick_refreshText(null);

        } else if (id == R.id.button_equal) {

            int n1 = Integer.parseInt(number1);
            int n2 = Integer.parseInt(number2);
            String result = String.valueOf(operation.equals("+") ? n1 + n2 : n1 - n2);
            onClick_refreshText(result);
            number1 = "0";
            operation = "";
            number2 = "0";

        } else if (id == R.id.button_plus || id == R.id.button_minus) {

            operation = text;

        } else if (id == R.id.button_0 || id == R.id.button_1 || id == R.id.button_2 || id == R.id.button_3 || id == R.id.button_4 || id == R.id.button_5 || id == R.id.button_6 || id == R.id.button_7 || id == R.id.button_8 || id == R.id.button_9) {

            if (operation.isEmpty()) {
                number1 = (number1.equals("0") ? "" : number1) + text;
                onClick_refreshText(null);
            } else {
                number2 = (number2.equals("0") ? "" : number2) + text;
                onClick_refreshText(null);
            }

        }

    }

    private void onClick_refreshText(String result) {
        String text = operation.isEmpty() ? number1 : number1 + operation + number2;
        if (result != null) text += "=" + result;
        tv_result.setText(text);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

}